<?php
namespace Home\Controller;

use Think\Controller;

/**
 * 评价管理
 * @author DONGGUIYU
 *
 */
class CommentController extends Controller
{
    /**
     * 评价列表
     */
    public function commentList(){
    	// 订单号
    	$orderId = I('post.orderNum', "", 'htmlspecialchars');
    	if(intval($orderId) > 0){
    		$where['order_id'] = $orderId;
    		$this->assign("orderNum", $orderId);
    	}
    	// 评价MODEL
    	$commentModel = D('Comment');
    	// 分页
    	$page = setPage($commentModel, $where);
    	// 进行分页数据查询
    	$orderby['id']='asc';
    	// 入口城市
    	if(isset($where)){
    	    $commentList = $commentModel->where($where)->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
    	} else {
    	    $commentList = $commentModel->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
    	}
    	$comment_list = array();
    	foreach ($commentList as $comment){
    	    $item = $comment;
    	    if($comment['product_type'] == 1){
    	        // 景点
    	        $attractionModel = D('Attraction');
    	        unset($where);
    	        $where['id'] = $comment['product_id'];
    	        $product_name = $attractionModel->where($where)->getField('name');
    	        $item['product_name'] = $product_name;
    	    } else if($comment['product_type'] == 2){
    	        // 周边游
    	        $atroundModel = D('Around');
    	        unset($where);
    	        $where['id'] = $comment['product_id'];
    	        $product_name = $atroundModel->where($where)->getField('name');
    	        $item['product_name'] = $product_name;
    	    }
    	    unset($where);
    	    $where['id'] = $comment['user_id'];
    	    $userModel = D('User');
    	    $user = $userModel->where($where)->select();
    	    if(!empty($user[0]['username']) && $user[0]['username'] != $user[0]['phone']){
    	        $item['comment_user_name'] = $user[0]['username'];
    	    } else{
    	        $item['comment_user_name'] = $user[0]['user_code'];
    	    }
    	    $comment_list[] = $item;
    	}
    	$show = $page->show();// 分页显示输出
    	$this->assign('page',$show);// 赋值分页输出
    	$this->assign("commentList", $comment_list);
        $this->display('comment');
    }
    
    /**
     * 编辑评价
     */
    public function editComment(){
    	$this->assign('title','审核评论');
    	// 评价ID
    	$id = I('get.id', "", 'htmlspecialchars');
    	// 评价model
    	$commentModel = D('Comment');
    	$where['id'] = $id;
    	$rlt = $commentModel->where($where)->select();
    	if(isset($rlt)){
    		$this-> assign('comment', $rlt[0]);
    	}
    	$this->display('checkcomment');
    }
    
    /**
     * 评论审核通过
     */
    public function checkComment(){
        $id = I('get.id', "", 'htmlspecialchars');
        $commentModel = D('Comment');
        $where['id']= $id;
        $data['has_checked'] = 1;
        $data['check_time'] = date('Y-m-d H:i:s');
        $rlt = $commentModel->where($where)->save($data);
        if($rlt > 0){
            $this->success('评论审核成功！','commentList');
            exit;
        } else {
            $this->error('评论审核失败');
            exit;
        }
    }
    
    /**
     * 评论审核不通过
     */
    public function refuseComment(){
        $id = I('get.id', "", 'htmlspecialchars');
        $commentModel = D('Comment');
        $where['id']= $id;
        $rlt = $commentModel->where($where)->select();
        if(!empty($rlt)){
            $comment_user_id = $rlt[0]['user_id'];
            $comment_time = $rlt[0]['comment_time'];
            if(!empty($comment_user_id)){
                $messageModel = D('Message');
                $data['content'] = "您于".$comment_time."发表的评论未通过审核!";
                $data['accept_user_id'] = $comment_user_id;
                $data['send_user_id'] = 0;
                $data['send_time'] = date('Y-m-d H:i:s');
                $data['has_read'] = 0;
                $messageModel->startTrans();
                $rs = $messageModel->data($data)->add();
                if($rs > 0){
                    $rs = $commentModel->where($where)->delete();
                    if($rs > 0){
                        $messageModel->commit();
                        $this->success('该评论已被成功删除！','commentList');
                        exit;
                    } else {
                        $commentModel->rollback();
                        $this->error('评论审核失败');
                        exit;
                    }
                } else {
                    $commentModel->rollback();
                    $this->error('评论审核失败');
                    exit;
                }
            } else {
                $this->error('评论审核失败!');
                exit;
            }
        } else {
            $this->error('该评论已被删除!');
            exit;
        }
    }
    
    
    /**
     * 保存评价
     */
    public function saveComment(){
        // 评价ID
        $id = I('post.id', "", 'htmlspecialchars');
    	// 评价model
    	$commentModel = D('Comment');
    	$data['content'] = I('post.content',"", 'htmlspecialchars');
    	$commentModel->startTrans();
    	if(empty($id)){
    		// 添加订单
    		$rlt = $commentModel->data($data)->add();
    	} else {
    		// 修改订单
    		$where['id'] = $id;
    		$rlt = $commentModel->where($where)->save($data);
    	}
    	if($rlt != 0 && $rlt == false){
    		$commentModel->rollback();
    		$this->error("保存失败！");
    		exit;
    	} else {
    		$commentModel->commit();
    		$this->success("保存成功！", "commentList");
    		exit;
    	}
    }
    
    /**
     * 删除评论
     */
    public function deleteComment(){
    	$idList = I('post.idList', "", 'htmlspecialchars');
    	// 订单model
    	$commentModel = D('Comment');
    	$commentModel->startTrans();
    	if(!empty($idList)){
    		$idArray = split(',', $idList);
    		// 开启事务
    		$commentModel->startTrans();
    		foreach ($idArray as $id){
    			$where['id'] = $id;
    			$rlt = $commentModel->where($where)->delete();
    			if($rlt == false){
    				// 删除失败时回滚事务
    				$commentModel->rollback();
    				$this->error("删除失败！");
    				exit;
    			}
    		}
    		// 提交事务
    		$commentModel->commit();
    		$this->success("删除成功！", 'commentList');
    	}
    }
}

?>